<?php

class PaymentBuyPrime extends PaymentProcess {
	
	protected $_earnedbidlogid;
	
	protected $_paymentsource;
	
	public function save(){
		$db = Yii::app()->db->beginTransaction();
		try{
			$paymentMenthod = $this->getProcessInfo();
			$prime = $this->prime->toArray();
			$userPrime = UserPrime::model()->findByPk($this->customer->id);
			$userPrime = is_object($userPrime) ? $userPrime : new UserPrime();
			$expired = empty($userPrime->expired) ? strtotime('tomorrow') : $userPrime->expired;
			$userPrime->expired = strtotime("+{$prime['primeYear']} year",$expired);
			$userPrime->id = $this->customer->id;
			$userPrime->save();
			$db->commit();
			//重置过期时间
			Yii::app()->user->getIsPrime(true);
		} catch (Exception $ex){
			$db->rollback();
			$model = new PaymentLog();
			$model->type = $paymentMenthod['paymentType'];
			$model->request = $this->getPaymentCode();
			$model->customer_id = Yii::app()->user->id;
			$model->created = time();
			$model->response = json_encode($ex->getMessage());
			$model->save();
		}
		return $this;
	}
	
	public function redirect(){
		Yii::app()->request->redirect(Yii::app()->createUrl('prime/detail'));
	}

}